package com.shuoyue.ycgk.ui.papergroups.special;

import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.SpecialType;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

/**
 * 专项训练适配
 */
public class SpecialChildAdapter extends AppBaseQuickAdapter<SpecialType> {
    OnItemChildClickListener listener;

    public SpecialChildAdapter(@Nullable List data, OnItemChildClickListener onItemChildClickListener) {
        super(R.layout.item_special, data);
        this.listener = onItemChildClickListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, SpecialType item) {
        helper.addOnClickListener(R.id.lay_all);
        helper.addOnClickListener(R.id.start);
        helper.addOnClickListener(R.id.indicator);
        helper.setVisible(R.id.indicator, item.getChildren() != null && item.getChildren().size() > 0);
        helper.setText(R.id.title, item.getName());
        helper.setText(R.id.done, item.getDoCount() + "");
        helper.setText(R.id.total, item.getCount() + "");
        int rate = item.getCount() == 0 ? 0 : item.getDoCount() * 100 / item.getCount();
        helper.setImageResource(R.id.indicator, item.isSelect() ? R.mipmap.arrow_circle_up : R.mipmap.arrow_circle_down);

        helper.setImageResource(R.id.p1, R.mipmap.spe_progress_n);
        helper.setImageResource(R.id.p2, R.mipmap.spe_progress_n);
        helper.setImageResource(R.id.p3, R.mipmap.spe_progress_n);
        helper.setImageResource(R.id.p4, R.mipmap.spe_progress_n);
        helper.setImageResource(R.id.p5, R.mipmap.spe_progress_n);
        if (rate > 0) {
            helper.setImageResource(R.id.p1, R.mipmap.spe_progress_y);
        }
        if (rate > 20) {
            helper.setImageResource(R.id.p2, R.mipmap.spe_progress_y);
        }
        if (rate > 40) {
            helper.setImageResource(R.id.p3, R.mipmap.spe_progress_y);
        }
        if (rate > 60) {
            helper.setImageResource(R.id.p4, R.mipmap.spe_progress_y);
        }
        if (rate > 80) {
            helper.setImageResource(R.id.p5, R.mipmap.spe_progress_y);
        }
        RecyclerView recyclerView = helper.getView(R.id.childRecycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        if (recyclerView.getTag() == null) {
            recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
            recyclerView.setTag("tag");
        }
        if (item.isSelect() && item.getChildren() != null && item.getChildren().size() > 0) {
            recyclerView.setVisibility(View.VISIBLE);
            SpecialChildAdapter specialAdapter = new SpecialChildAdapter(item.getChildren(), listener);
            recyclerView.setAdapter(specialAdapter);
            specialAdapter.setOnItemChildClickListener(listener);
        } else {
            recyclerView.setVisibility(View.GONE);

        }
        if(item.getChildren()==null||item.getChildren().size()==0){
            TextView text = helper.getView(R.id.title);
            text.setTypeface(null, Typeface.BOLD);
        }
    }

}
